//-----------------------------------------------------------------------
// <copyright file="CatalogDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Collections;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MIMS.WRS.Entities;

namespace MIMS.WRS.Data
{    
    public abstract class CatalogDAOBase
    {
        #region Common methods
        public virtual Catalog CreateCatalogFromReader(IDataReader reader)
        {
            Catalog item = new Catalog();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("CatalogID"))) item.CatalogID = (int)reader["CatalogID"];
                if (!reader.IsDBNull(reader.GetOrdinal("CatalogName"))) item.CatalogName = (string)reader["CatalogName"];
                if (!reader.IsDBNull(reader.GetOrdinal("CatalogOrder"))) item.CatalogOrder = (int)reader["CatalogOrder"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCatalogFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateCatalog methods
            
        public virtual void CreateCatalog(Catalog catalog)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCatalogsInsert");
                
                dbCommandWrapper.AddInParameter("@CatalogName", DbType.String, catalog.CatalogName);
                dbCommandWrapper.AddInParameter("@CatalogOrder", DbType.Int32, catalog.CatalogOrder);
                dbCommandWrapper.AddOutParameter("@CatalogID", DbType.Int32, 0);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
                catalog.CatalogID = (int)dbCommandWrapper.GetParameterValue("@CatalogID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCatalogException, ex);
            }
        }

        #endregion

        #region UpdateCatalog methods
        
        public virtual void UpdateCatalog(Catalog catalog)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCatalogsUpdate");            
                
                dbCommandWrapper.AddInParameter("@CatalogID", DbType.Int32, catalog.CatalogID);
                dbCommandWrapper.AddInParameter("@CatalogName", DbType.String, catalog.CatalogName);
                dbCommandWrapper.AddInParameter("@CatalogOrder", DbType.Int32, catalog.CatalogOrder);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateCatalogException, ex);
            }
        }
        
        #endregion

        #region DeleteCatalog methods
        public virtual void DeleteCatalog(int catalogID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCatalogsDelete");
                
                dbCommandWrapper.AddInParameter("@CatalogID", DbType.Int32, catalogID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteCatalogException, ex);
            }
        }

        #endregion

        #region GetCatalog methods
        
        public virtual Catalog GetCatalog(int catalogID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCatalogsGet");
                
                dbCommandWrapper.AddInParameter("@CatalogID", DbType.Int32, catalogID);
                
                Catalog catalog =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    if (reader.Read())
                    {
                        catalog = CreateCatalogFromReader(reader);
                        reader.Close();
                    }
                }
                return catalog;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCatalogException, ex);
            }
        }
        
        #endregion

        #region GetCatalogs methods
        public virtual CatalogCollection GetCatalogs(CatalogColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCatalogsGetList");
                
                dbCommandWrapper.AddInParameter("@OrderBy", DbType.AnsiString, orderBy.ToString());
                dbCommandWrapper.AddInParameter("@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                dbCommandWrapper.AddInParameter("@Page", DbType.Int32, page);
                dbCommandWrapper.AddInParameter("@PageSize", DbType.Int32, pageSize);
                dbCommandWrapper.AddOutParameter("@TotalRecords", DbType.Int32, 4);
                
                CatalogCollection catalogCollection = new CatalogCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    while (reader.Read())
                    {
                        Catalog catalog = CreateCatalogFromReader(reader);
                        catalogCollection.Add(catalog);
                    }
                    reader.Close();
                }
                totalRecords = (int)dbCommandWrapper.GetParameterValue("@TotalRecords");
                return catalogCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCatalogsException, ex);
            }
        }
        
        public virtual CatalogCollection GetCatalogs(CatalogColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetCatalogs(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}